package com.ydgk.dao;

import com.ydgk.bean.Admin;
import com.ydgk.bean.Book;
import com.ydgk.db.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;

public class AdminDao{
    protected JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public Admin findByName(String name){
        String sql = "select * from admin where aname =?" ;
        List<Admin> list = template.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),name);
        return list.size() > 0 ? list.get(0) : null ;
    }

    public int updateById(Admin admin){
        return template.update("update admin set aname = ?, apass = ? where aid = ?",
                admin.getAname(),
                admin.getApass(),
                admin.getAid());
    }

    public Admin findById(int id){
        String sql = "select * from admin where aid =?" ;
        List<Admin> list = template.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),id);
        return list.size() > 0 ? list.get(0) : null ;
    }

    public int findAllNumber(){
        String sql = "select count(*) from admin";
        long t = template.queryForObject(sql, Long.class);
        int total = (int)t;//将long类型转换成int类型
        return total;
    }
}
